/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.controller;

import com.techshop.model.Order;
import com.techshop.model.OrderDetail;
import com.techshop.model.PhoneCodeStore;
import com.techshop.model.Store;
import com.techshop.service.OrderService;
import com.techshop.service.OrderDetailService;
import com.techshop.service.PhoneCodeStoreService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author Hai-PC
 */
@ManagedBean(name = "deliveryNoteController")
@ViewScoped
public class DeliveryNoteController extends AbstractController
{
    private Order selectedDeliveryNote;
    private OrderDetail selectedDeliveryNoteDetails;
    private List<Order> deliveryNoteList;
    private List<OrderDetail> deliveryNoteDetailsList;
    private List<Order> filterDeliveryNotes;
    
    @ManagedProperty(value = "#{orderService}")
    private OrderService orderService;
    @ManagedProperty(value = "#{orderDetailService}")
    private OrderDetailService orderDetailService;
    @ManagedProperty(value = "#{phoneCodeStoreService}")
    private PhoneCodeStoreService phoneCodeStoreService;

    private String selectedPhone = "";
    private Date selectedDate;
    private boolean selectedState = false;
    private Store selectedStore = new Store();
    
    private boolean isSearch = false;
    
    public PhoneCodeStoreService getPhoneCodeStoreService() {
        return phoneCodeStoreService;
    }

    public void setPhoneCodeStoreService(PhoneCodeStoreService phoneCodeStoreService) {
        this.phoneCodeStoreService = phoneCodeStoreService;
    }
    
    public void updateDeliveryNote()
    {
        isSearch = false;
        if (orderService.update(selectedDeliveryNote))
        {
            if (selectedDeliveryNote.isState())
            {
                // Update inventory
                List<OrderDetail> orderDetailList = orderDetailService.getByDeliveryNoteId(selectedDeliveryNote.getId());
                for (OrderDetail o : orderDetailList)
                {
                    PhoneCodeStore phoneCodeStore = phoneCodeStoreService.getByPhoneCodeAndStore(o.getPhoneCode().getId(), selectedDeliveryNote.getStore().getId());
                    if (phoneCodeStore == null) // ERROR
                    {
                    }
                    else
                    {
                        phoneCodeStore.setQuantity(phoneCodeStore.getQuantity() - o.getQuantity());
                        phoneCodeStoreService.update(phoneCodeStore);
                    }
                }
            }
            
            addInfoMessage("Cập nhật thành công");
        }
        else
        {
            addErrorMessage("Cập nhật thất bại");
        }
    }
    
    public void updateDeliveryNoteDetails()
    {
        isSearch = false;
        if (orderDetailService.update(selectedDeliveryNoteDetails))
        {
            Order order = orderService.getById(selectedDeliveryNoteDetails.getOrder().getId());
            List<OrderDetail> list = orderDetailService.getByDeliveryNoteId(order.getId());
            int total = 0;
            for (OrderDetail o : list)
            {
                total += o.getPrice() * o.getQuantity();
            }
            order.setTotal(total);
            if (orderService.update(order))
            {
                addInfoMessage("Cập nhật thành công");
            }
        }
        else
        {
            addErrorMessage("Cập nhật thất bại");
        }
    }
    
    public void search()
    {
        isSearch = true;
        if (selectedDate == null)
        {
            if (selectedStore.getId() != null)
            {
                setDeliveryNoteList(orderService.search(selectedPhone, "", selectedState, selectedStore.getId()));
            }
            else
            {
                setDeliveryNoteList(orderService.searchNotStore(selectedPhone, "", selectedState));
            }
        }
        else
        {
            String dateString = new SimpleDateFormat("yyyy-MM-dd").format(selectedDate);
            if (selectedStore.getId() != null)
            {
                setDeliveryNoteList(orderService.search(selectedPhone, dateString, selectedState, selectedStore.getId()));
            }
            else
            {
                setDeliveryNoteList(orderService.searchNotStore(selectedPhone, dateString, selectedState));
            }
        }
    }
    
    public List<Order> getDeliveryNoteList()
    {
        if (!isSearch)
        {
            setDeliveryNoteList(orderService.getAll());
        }
        return deliveryNoteList;
    }

    public void setDeliveryNoteList(List<Order> deliveryNoteList)
    {
        this.deliveryNoteList = deliveryNoteList;
    }

    public OrderService getOrderService()
    {
        return orderService;
    }

    public void setOrderService(OrderService orderService)
    {
        this.orderService = orderService;
    }
    
    public Order getSelectedDeliveryNote()
    {
        return selectedDeliveryNote;
    }

    public void setSelectedDeliveryNote(Order selectedDeliveryNote)
    {
        this.selectedDeliveryNote = selectedDeliveryNote;
    }

    public List<Order> getFilterDeliveryNotes()
    {
        return filterDeliveryNotes;
    }

    public void setFilterDeliveryNotes(List<Order> filterDeliveryNotes)
    {
        this.filterDeliveryNotes = filterDeliveryNotes;
    }
    
    public OrderDetail getSelectedDeliveryNoteDetails()
    {
        return selectedDeliveryNoteDetails;
    }

    public void setSelectedDeliveryNoteDetails(OrderDetail selectedDeliveryNoteDetails)
    {
        this.selectedDeliveryNoteDetails = selectedDeliveryNoteDetails;
    }
    
    public List<OrderDetail> getDeliveryNoteDetailsList()
    {
        if (selectedDeliveryNote != null)
        {
            setDeliveryNoteDetailsList(orderDetailService.getByDeliveryNoteId(selectedDeliveryNote.getId()));
        }
        return deliveryNoteDetailsList;
    }

    public void setDeliveryNoteDetailsList(List<OrderDetail> deliveryNoteDetailsList)
    {
        this.deliveryNoteDetailsList = deliveryNoteDetailsList;
    }

    public OrderDetailService getOrderDetailService()
    {
        return orderDetailService;
    }

    public void setOrderDetailService(OrderDetailService orderDetailService)
    {
        this.orderDetailService = orderDetailService;
    }
    
    public void deleteDeliveryNote()
    {
        isSearch = false;
        List<OrderDetail> list = orderDetailService.getByDeliveryNoteId(selectedDeliveryNote.getId());
        for (OrderDetail d : list)
        {
            if (!orderDetailService.delete(d))
            {
                addErrorMessage("Xóa thất bại");
                return;
            }
        }
        if (orderService.delete(selectedDeliveryNote))
        {
            addInfoMessage("Xóa thành công");
        }
        else
        {
            addErrorMessage("Xóa thất bại");
        }
    }
    
    public void deleteDeliveryNoteDetails()
    {
        isSearch = false;
        if (orderDetailService.delete(selectedDeliveryNoteDetails))
        {
            addInfoMessage("Xóa thành công");
        }
        else
        {
            addErrorMessage("Xóa thất bại");
        }
    }

    public String getSelectedPhone() {
        return selectedPhone;
    }

    public void setSelectedPhone(String selectedPhone) {
        this.selectedPhone = selectedPhone;
    }

    public Date getSelectedDate() {
        return selectedDate;
    }

    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }

    public boolean isSelectedState() {
        return selectedState;
    }

    public void setSelectedState(boolean selectedState) {
        this.selectedState = selectedState;
    }

    public Store getSelectedStore() {
        return selectedStore;
    }

    public void setSelectedStore(Store selectedStore) {
        this.selectedStore = selectedStore;
    }
}